package com.yinshan.voiceplay.utils;

import android.content.Context;
import android.util.Log;

import com.yinshan.voiceplay.R;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author limh
 * @function 金额转中文
 * @date 2019/1/4 11:51
 */
public class MoneyUtils {
    private static final char[] NUM = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    private static final char[] CHINESE_UNIT = {'元', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿', '拾', '佰', '仟'};
    private static final Integer[] numVoice = {R.raw.tts_0, R.raw.tts_1, R.raw.tts_2, R.raw.tts_3,
            R.raw.tts_4, R.raw.tts_5, R.raw.tts_6, R.raw.tts_7, R.raw.tts_8, R.raw.tts_9};

    /**
     * 返回关于钱的中文式大写数字,仅支持到亿
     */
    private static String readInt(int moneyNum) {
        StringBuilder res = new StringBuilder();
        int i = 0;
        if (moneyNum == 0) {
            return "0";
        }

        if (moneyNum == 10) {
            return "拾";
        }

        if (moneyNum > 10 && moneyNum < 20) {
            return "拾" + moneyNum % 10;
        }

        while (moneyNum > 0) {
            res.insert(0, CHINESE_UNIT[i++]);
            res.insert(0, NUM[moneyNum % 10]);
            moneyNum /= 10;
        }
        String str = res.toString().replaceAll("0[拾佰仟]", "0")
                .replaceAll("0+亿", "亿")
                .replaceAll("0+万", "万")
                .replaceAll("0+元", "元")
                .replaceAll("0+", "0")
                .replace("元", "");
        Log.d("MoneyUtils", str);
        return str;
    }

    /**
     * 返回数字对应的音频id索引
     *
     * @param integerPart 字符串（如：4拾5万）
     * @return 音频索引数组
     */
    public static List<Integer> readIntPart(String integerPart) {
        List<Integer> result = new ArrayList<>();
        String intString = readInt(Integer.parseInt(integerPart));
        int len = intString.length();
        for (int i = 0; i < len; i++) {
            char current = intString.charAt(i);
            if (current == '拾') {
                result.add(R.raw.tts_ten);
            } else if (current == '佰') {
                result.add(R.raw.tts_hundred);
            } else if (current == '仟') {
                result.add(R.raw.tts_thousand);
            } else if (current == '万') {
                result.add(R.raw.tts_ten_thousand);
            } else if (current == '亿') {
                result.add(R.raw.tts_ten_million);
            } else {
                result.add(numVoice[Integer.parseInt(String.valueOf(current))]);
            }
        }
        return result;
    }

    public static List<String> readIntPart(Context context, String integerPart) {
        List<String> result = new ArrayList<>();
        String intString = readInt(Integer.parseInt(integerPart));
        int len = intString.length();
        for (int i = 0; i < len; i++) {
            char current = intString.charAt(i);
            if (current == '拾') {
                result.add(context.getResources().getResourceName(R.raw.tts_ten));
            } else if (current == '佰') {
                result.add(context.getResources().getResourceName(R.raw.tts_hundred));
            } else if (current == '仟') {
                result.add(context.getResources().getResourceName(R.raw.tts_thousand));
            } else if (current == '万') {
                result.add(context.getResources().getResourceName(R.raw.tts_ten_thousand));
            } else if (current == '亿') {
                result.add(context.getResources().getResourceName(R.raw.tts_ten_million));
            } else {
                result.add(context.getResources().getResourceName(numVoice[Integer.parseInt(String.valueOf(current))]));
            }
        }
        return result;
    }

    /**
     * 返回数字对应的音频id索引
     *
     * @param decimalPart 字符串（如：86）
     * @return 音频索引数组
     */
    public static List<Integer> readDecimalPart(String decimalPart) {
        List<Integer> result = new ArrayList<>();
        if (!"00".equals(decimalPart)) {
            char[] chars = decimalPart.toCharArray();
            for (char ch : chars) {
                result.add(numVoice[Integer.parseInt(String.valueOf(ch))]);
            }
        }
        return result;
    }

    public static List<String> readDecimalPart(Context context, String decimalPart) {
        List<String> result = new ArrayList<>();
        if (!"00".equals(decimalPart)) {
            char[] chars = decimalPart.toCharArray();
            for (char ch : chars) {
                result.add(context.getResources().getResourceName(numVoice[Integer.parseInt(String.valueOf(ch))]));
            }
        }
        return result;
    }

    /**
     * 提取字符串中的 数字 带小数点 ，没有就返回""
     *
     * @param money 字符串
     * @return 金额
     */
    public static String getMoney(String money) {
        Pattern pattern = Pattern.compile("(\\d+\\.\\d+)");
        Matcher m = pattern.matcher(money);
        if (m.find()) {
            money = m.group(1) == null ? "" : m.group(1);
        } else {
            pattern = Pattern.compile("(\\d+)");
            m = pattern.matcher(money);
            if (m.find()) {
                money = m.group(1) == null ? "" : m.group(1);
            } else {
                money = "";
            }
        }

        return money;
    }
}
